// import * as Cesium from '@cesium/Source/Cesium.js';

import createODLinesPrimitive from './createODLinesPrimitive';

const $defaultOptions = {
  data: [],
  color: [1.0, 1.0, 0.0, 1.0],
  show: true,
  timeDuration: 5.0, // 单位秒
  currentTime: 0.0,
  loopPlay: true,
  translucentPass: true
};

class ODLines {

  constructor(viewer, options) {
    this.viewer = viewer;
    this.options = {
      ...$defaultOptions,
      ...options
    };
    this.init();
  }

  init() {
    this._playingListener = undefined
  }

  get data() {
    return this.options.data
  }
  get color() {
    return this.options.color
  }
  get enabled() {
    return this.options.enabled
  }
  get show() {
    return this.options.show
  }
  get loopPlay() {
    return this.options.loopPlay
  }
  get translucentPass() {
    return this.options.translucentPass
  }
  get currentTime() {
    return this.options.currentTime
  }
  get timeDuration() {
    return this.options.timeDuration
  }
  get playing() {
    return !!this._playingListener;
  }

  set data(val) {
    this.options.data = val;
    this.updatePrimitive2();
  }
  set color(val) {
    this.options.color = val
  }
  set enabled(val) {
    this.options.enabled = val
  }
  set show(val) {
    this.options.show = val
  }
  set loopPlay(val) {
    this.options.loopPlay = val
  }
  set translucentPass(val) {
    this.options.translucentPass = val;
    this.updatePrimitive2();
  }
  set currentTime(val) {
    this.options.currentTime = val
  }
  set timeDuration(val) {
    this.options.timeDuration = val
  }
  set playing(val) {
    if (!!val && !this._playingListener) {
      this._play();
    } else if (!val && this._playingListener) {
      this._playingListener = this._playingListener && this._playingListener();
    }
  }

  updateTimeRatio() {
    const _this = this;
    return function(instanceIndex, frameState, cfg) {
      const instData = _this.data[instanceIndex];
      const {
        startTime: st,
        duration: dr,
        color,
        repeat
      } = instData;
      var time = _this.currentTime;
      var diff = time > st ? time - st : time + _this.timeDuration - st;
      cfg.timeRatio = Math.min(diff / dr, 0x1);
      color && cfg.color.splice.apply(cfg.color, [0x0, 0x4].concat(color));
      repeat && (cfg.repeat = repeat);
      return cfg;
    };
  }

  updatePrimitive() {
    const _this = this;
    const {
      color,
      show,
      _primitive
    } = this;
    if (_primitive) {
      _primitive.show = show;
      var uniformsColor = _primitive.appearance.material.uniforms.color;
      uniformsColor.red = color[0];
      uniformsColor.green = color[1];
      uniformsColor.blue = color[2];
      uniformsColor.alpha = color[3];
    }
  };

  updatePrimitive2() {
    const _this = this;
    var primitives = this.viewer.scene.primitives;
    if (_this._primitive) {
      primitives.remove(_this._primitive);
      _this._primitive = undefined;
    }

    const {
      data,
      color,
      translucentPass
    } = this.options
    if (data) {
      var primitive = createODLinesPrimitive(data, color, _this.updateTimeRatio(), translucentPass);
      _this._primitive = primitives.add(primitive);

      // data.splice(0);
    }
    this.updatePrimitive();
  };

  _play() {
    var _this2 = this;
    var scene = this.viewer.scene;

    // 如果位于末端，则从头开始
    if (this.currentD === this.timeDuration) {
      this.currentD = 0;
    }

    var lastTimeStamp = Date.now();
    this._playingListener = scene.postUpdate.addEventListener(function(scene, time) {
      var currTimestamp = Date.now();
      var diffTimeStamp = currTimestamp - lastTimeStamp;
      lastTimeStamp = currTimestamp;
      var currentTime = _this2.currentTime + diffTimeStamp * 0.001;
      if (_this2.loopPlay) {
        if (currentTime > _this2.timeDuration) {
          _this2.currentTime = 0;
        } else {
          _this2.currentTime = currentTime;
        }
      } else {
        if (currentTime > _this2.timeDuration) {
          _this2.currentTime = _this2.timeDuration;
          _this2._playingListener && _this2._playingListener();
        } else {
          _this2.currentTime = currentTime;
        }
      }
    }, this);
  }

  destroy() {
    const {
      scene
    } = this.viewer;
    this._playingListener && scene.postUpdate.removeEventListener(this._playingListener, this)
    if(this.data){
      this.data.splice(0);
      this.data = undefined
    }
  }
}

export default ODLines
